//
// ScoreBoard.as
//
// Copyright (c) 2009 Nathan Barnes
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//
// A game state data display for SimpleAVRG.

package {

import com.threerings.flash.SimpleTextButton;
import com.threerings.util.Log;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;


public class ScoreBoard extends Sprite
{
    public static const WIDTH :int = 150;
    public static const HEIGHT :int = 100;

    // The base X position of the text fields.
    public static const FIELD_LEFT :int = 5;

    // The base Y position and height (including padding) of the text fields.
    public static const FIELD_TOP :int = 10;
    public static const FIELD_HEIGHT :int = 15;

    /**
     * Create a new score board.
     */
    public function ScoreBoard ()
    {
        buildDisplay();
    }

    /**
     * Set the display for the total number of monsters in the game to the given value.
     */
    public function setGlobalMonsterCount (monsters :int) :void
    {
        _globalMonsterCountDisplay.text = "Monsters in game: " + monsters;
    }

    /**
     * Set the display for the number of monsters in the room.
     */
    public function setRoomMonsterCount (monsters :int) :void
    {
        _roomMonsterCountDisplay.text = "Monsters in room: " + monsters;
    }

    /**
     * Set the display for the number of monsters in the player's basket.
     */
    public function setBasketMonsterCount (monsters :int) :void
    {
        _basketMonsterCountDisplay.text = "Monsters in basket: " + monsters;
    }

    // Set up the display objects.
    protected function buildDisplay () :void
    {
        graphics.beginFill(0x666666);
        graphics.drawRoundRect(0, 0, WIDTH, HEIGHT, 3, 3);
        graphics.endFill();

        _globalMonsterCountDisplay = new TextField();
        initCounterDisplay(_globalMonsterCountDisplay, "Monsters in game");

        _roomMonsterCountDisplay = new TextField();
        initCounterDisplay(_roomMonsterCountDisplay, "Monsters in room");

        _basketMonsterCountDisplay = new TextField();
        initCounterDisplay(_basketMonsterCountDisplay, "Monsters in basket");
    }

    // Set the label and position of a counter display.
    protected function initCounterDisplay (field :TextField, label :String) :void
    {
        field.autoSize = TextFieldAutoSize.LEFT;
        field.text = label + ": 0"; // This will usually be immediately updated.
        field.x = FIELD_LEFT;
        field.y = FIELD_TOP + FIELD_HEIGHT * _countDisplaysAdded++;
        addChild(field);
    }

    // This is called when the "catch a monster" button has been clicked.
    protected function handleCatchMonsterClick (event :MouseEvent) :void
    {
        dispatchEvent( new ScoreBoardEvent(ScoreBoardEvent.CATCH_MONSTER_REQUEST) );
    }

    // The display for the total number of monsters in the game.
    protected var _globalMonsterCountDisplay :TextField;

    // The display for the number of monsters in the room.
    protected var _roomMonsterCountDisplay :TextField;

    // The display for the number of monsters in the player's basket.
    protected var _basketMonsterCountDisplay :TextField;

    // A counter of the number of text fields added so far.
    protected var _countDisplaysAdded :int = 0;

    // A button for catching monsters.
    protected var _catchMonsterButton :SimpleTextButton;

    // The logger.
    protected var _log :Log = Log.getLog(ScoreBoard);
}

}

